import {getAllRedPkgList, getRedPkg, checkHasGetRedPkg} from 'config/api'
import router from 'umi/router'
export default {
  namespace: 'redPkgList',
  state:{
    redPkgList: [],
    hasGeted: false,
    currentRedPkg: {},
  },
  effects:{
    *getAllRedPkgListEff(action,{call,put}){
     let {list} = yield call(getAllRedPkgList)
     yield put({
       type: 'save',
       payload: {redPkgList: list}
     })
    },
    *getRedPkgEff({id},{call,put}){
      let {code} = yield call(getRedPkg,{id})
      if(code === -1){

      }else{
        router.push('/redPkgRecord')
      }
    },
    *checkHasGetRedPkgEff({id, callback},{call,put}){
      let {hasGeted,canGet} = yield call(checkHasGetRedPkg,id)
      yield put({
        type: 'save',
        payload: {hasGeted}
      })
      callback(hasGeted,canGet)
    }
  },
  reducers:{
    save(state,{payload}){
      return {...state,...payload}
    }
  }
}
